home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / templatehelper.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  2.4 KB  |  86 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import cPickle
  5. import inspect
  6. import shutil
  7. import re
  8. import traceback
  9. import random
  10. import types
  11. import logging
  12. from itertools import izip
  13. from xhtmltools import urlencode
  14. HTMLPattern = re.compile('^.*<body.*?>(.*)</body\\s*>', re.S)
  15. attrPattern = re.compile('^(.*?)@@@(.*?)@@@(.*)$')
  16. resourcePattern = re.compile('^resource:(.*)$')
  17. rawAttrPattern = re.compile('^(.*?)\\*\\*\\*(.*?)\\*\\*\\*(.*)$')
  18. _unicache = { }
  19. _escapecache = { }
  20.  
  21. def quoteattr(orig):
  22.     orig = unicode(orig)
  23.     return orig.replace(u'"', u'"')
  24.  
  25.  
  26. def escape(orig):
  27.     orig = unicode(orig)
  28.     
  29.     try:
  30.         return _escapecache[orig]
  31.     except:
  32.         _escapecache[orig] = orig.replace('&', '&').replace('<', '<').replace('>', '>')
  33.         return _escapecache[orig]
  34.  
  35.  
  36. defaultEncoding = 'iso-8859-1'
  37. _utf8cache = { }
  38.  
  39. def toUTF8Bytes(string, encoding = None):
  40.     
  41.     try:
  42.         return _utf8cache[(string, encoding)]
  43.     except KeyError:
  44.         result = None
  45.         if isinstance(string, types.UnicodeType):
  46.             result = string.encode('utf-8')
  47.         elif not isinstance(string, types.StringType):
  48.             string = str(string)
  49.         
  50.         if result is None and encoding is not None:
  51.             
  52.             try:
  53.                 decoded = string.decode(encoding, 'replace')
  54.             except (UnicodeDecodeError, ValueError, LookupError):
  55.                 pass
  56.  
  57.             result = decoded.encode('utf-8')
  58.         
  59.         if result is None:
  60.             result = string.decode(defaultEncoding, 'replace').encode('utf-8')
  61.         
  62.         _utf8cache[(string, encoding)] = result
  63.         return _utf8cache[(string, encoding)]
  64.  
  65.  
  66.  
  67. def toUni(orig, encoding = None):
  68.     
  69.     try:
  70.         return _unicache[orig]
  71.     except:
  72.         if isinstance(orig, types.UnicodeType):
  73.             return orig
  74.         elif not isinstance(orig, types.StringType):
  75.             _unicache[orig] = unicode(orig)
  76.         else:
  77.             orig = toUTF8Bytes(orig, encoding)
  78.             _unicache[orig] = unicode(orig, 'utf-8')
  79.         return _unicache[orig]
  80.  
  81.  
  82.  
  83. def generateId():
  84.     return 'tmplcomp%08d' % random.randint(0, 99999999)
  85.  
  86.